package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IMdProModelService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdProModel;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/promodel")
@Slf4j
public class MdProModelController extends BaseController {

    @Autowired
    private IMdProModelService mdProModelService;

    /**
     * 新增记录
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("c")
    public Resp create(MdProModel mdProModel) {

        Resp resp = new Resp();

        //验证型号是否重复 品名-定量-规格
        resp = mdProModelService.existence(mdProModel);
        if (Constant.FAIL.equals(resp.getCode())) {
            return resp;
        }

        mdProModel.preInsert();
        mdProModel.setActiveFlag("1");
        // 品名转大写
        String upCase = mdProModel.getProductName().toUpperCase();

        String density = mdProModel.getDensity();
        String spec = mdProModel.getSpec();
        // 拼接型号字符串
        StringBuilder stb = new StringBuilder();
        stb.append(upCase);
        stb.append("-");stb.append(density);
        stb.append("-");stb.append(spec);
//			if (mdProModel.getMachineNumber() != null && mdProModel.getMachineNumber() != ""){
//				stb.append("(");
//				stb.append(mdProModel.getMachineNumber());
//				stb.append(")");
//			}
        // 型号、品名重新赋值
        mdProModel.setModel(stb.toString());
        mdProModel.setProductName(upCase);
        mdProModel.setCreateBy(getLoginName());
        mdProModel.setLastModifiedBy(getLoginName());
        mdProModel.setGmtCreate(new Date());
        resp = mdProModelService.create(mdProModel);
        resp.setMsg("添加成功");
        return resp;
    }

    /**
     * 批量新增
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("cc")
    public Resp createBatch(MdProModel mdProModel) {

        Resp resp = new Resp();

        // 根据生成条件中的品名、定量 查询 现有型号
        MdProModel obj = new MdProModel();
        obj.setProductName(mdProModel.getProductName());
        obj.setDensity(mdProModel.getDensity());
        List<MdProModel> objs = mdProModelService.findModel(obj);

        // 判断 即将批量生成的型号 与 现有型号 是否重复
        long min = mdProModel.getSpecMin();
        long max = mdProModel.getSpecMax();
        for (int i= 0;i<objs.size();i++){
            long x = Long.parseLong(objs.get(i).getSpec());
            if ( x > min && x < max){
                long e = x - min;
                if ( e % mdProModel.getSpecInterval() == 0 ){
                    resp.setCode(Constant.FAIL);
                    resp.setMsg("要创建的规格跨度内已有型号存在，请重新选择规格区间");
                    return resp;
                }
            }
        }

        mdProModel.preInsert();
        mdProModel.setActiveFlag("1");
        // 品名转大写
        String upCase = mdProModel.getProductName().toUpperCase();
        String density = mdProModel.getDensity();

        // 拼接型号字符串 品名-定量
//        StringBuilder stb = new StringBuilder(upCase);
//        stb.append("-" + density);
//        String stb = new String(upCase + "-" + density);

        for (long e = min; e<=max;){
        	String spec = String.valueOf(e);
            StringBuilder model = new StringBuilder();
            model.append(upCase);
            model.append("-");model.append(density);
            model.append("-");model.append(spec);
//            StringBuilder model = stb;
            // 型号、品名重新赋值
            mdProModel.setModel(model.toString());
            mdProModel.setProductName(upCase);
            mdProModel.setCreateBy(getLoginName());
            mdProModel.setLastModifiedBy(getLoginName());
            mdProModel.setGmtCreate(new Date());
            mdProModel.setSpec(spec);
            resp = mdProModelService.create(mdProModel);
            e = e + mdProModel.getSpecInterval();
        }

        resp.setMsg("添加成功");
        return resp;
    }

    /**
     * 删除记录（逻辑删除active_flag=0)
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("d")
    public TransResult delete(MdProModel mdProModel) {
        mdProModel.preUpdate();
        Resp resp = mdProModelService.delete(mdProModel);
        return fromResp(resp);
    }

    /**
     * 查询列表
     *
     * @param
     * @return
     */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, MdProModel mdProModel) {
        Pager<MdProModel> result = mdProModelService.findListByCondition(page, rows, mdProModel);
        return success(result);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
        MdProModel mdProModel = mdProModelService.findById(id);
        return success(mdProModel);
    }

    /**
     * 修改记录
     *
     * @param mdProModel
     * @return
     */
    @PostMapping("u")
    public TransResult update(MdProModel mdProModel) {
        mdProModel.preUpdate();
        StringBuilder model = new StringBuilder();
        model.append(mdProModel.getProductName());
        model.append("-");model.append(mdProModel.getDensity());
        model.append("-");model.append(mdProModel.getSpec());
        mdProModel.setModel(model.toString());
        mdProModel.setLastModifiedBy(getLoginName());
        Resp resp = mdProModelService.update(mdProModel);
        return fromResp(resp);
    }

    /**
     * @Title getModels
     * @Description 查询型号列表
     * @author wzhg
     * @date 2018年8月30日:上午10:11:11
     * @param page
     * @param rows
     * @param mdProModel
     * @return TransResult
     */
    @PostMapping("getModels/list")
    public TransResult getModels(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            MdProModel mdProModel) {
        mdProModel.setActiveFlag("1");
        Pager<MdProModel> result = mdProModelService.findListByCondition(page, rows, mdProModel);
        return success(result);
    }

}
